package com.bsbm.member.service.impl;

import com.bsbm.mappers.MemberMapper;
import com.bsbm.member.bean.MemberBean;
import com.bsbm.member.dao.MemberDao;
import com.bsbm.member.domain.Member;
import com.bsbm.member.service.MemberService;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by SABIR IQBAL on 3/23/14.
 */
public class MemberServiceImpl implements MemberService{

    private MemberDao memberDao;

    private MemberMapper memberMapper;

    @Override
    public void create(MemberBean bean) {

        Member domain = memberMapper.getDomainFromBean(bean);

        memberDao.create(domain);
    }

    @Override
    public List<MemberBean> search(MemberBean memberBean) {
        List<Member> members = memberDao.search(memberBean);
        List<MemberBean> beans = new ArrayList<MemberBean>();
        if(CollectionUtils.isEmpty(members)){
            return Collections.EMPTY_LIST;
        } else {
            for(Member m : members){
                beans.add(memberMapper.getBeanFromDomain(m));
            }
            return beans;
        }
    }

    @Override
    public MemberBean findById(String id) {
        Member member = memberDao.findById(Long.valueOf(id));
        MemberBean bean = null;
        if(member != null){
            bean = memberMapper.getBeanFromDomain(member);
        }
        return bean;
    }

    @Override
    public void delete(String id) {
        memberDao.delete(Long.valueOf(id));
    }

    public MemberDao getMemberDao() {
        return memberDao;
    }

    public void setMemberDao(MemberDao memberDao) {
        this.memberDao = memberDao;
    }

    public MemberMapper getMemberMapper() {
        return memberMapper;
    }

    public void setMemberMapper(MemberMapper memberMapper) {
        this.memberMapper = memberMapper;
    }
}
